<template>
  <div class="my-hotel-container">
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="5" animated />
    </div>
    
    <div v-else-if="error" class="error-container">
      <el-empty :description="errorMessage">
        <template #extra>
          <el-button type="primary" @click="initData">重试</el-button>
          <el-button v-if="isNewHotelOwner" type="success" @click="openCreateHotelDrawer">创建酒店</el-button>
        </template>
      </el-empty>
    </div>
    
    <div v-else class="hotel-navigation">
      <el-card class="hotel-card">
        <template #header>
          <div class="hotel-header">
            <div class="hotel-title">
              <h1>我的酒店管理</h1>
              <p v-if="hotelData">当前酒店: {{ hotelData.hotelName }}</p>
            </div>
          </div>
        </template>
        
        <div class="hotel-dashboard">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-card class="dashboard-card" @click="navigateTo('/my-hotel/detail')">
                <el-icon class="card-icon"><InfoFilled /></el-icon>
                <h3>酒店详情</h3>
                <p>查看和管理酒店基本信息</p>
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="dashboard-card" @click="navigateTo('/my-hotel/rooms')">
                <el-icon class="card-icon"><House /></el-icon>
                <h3>房间管理</h3>
                <p>添加、编辑和管理酒店房间</p>
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="dashboard-card" @click="navigateTo('/my-hotel/orders')">
                <el-icon class="card-icon"><Document /></el-icon>
                <h3>订单管理</h3>
                <p>查看和处理酒店订单</p>
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="dashboard-card" @click="navigateTo('/my-hotel/reviews')">
                <el-icon class="card-icon"><ChatLineRound /></el-icon>
                <h3>评价管理</h3>
                <p>查看客户评价</p>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </el-card>
    </div>
    
    <create-hotel v-if="isNewHotelOwner" ref="createHotelRef" @refresh="initData"/>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getHotelByUserId } from '@/api/hotel'
import { useUserInfoStore } from '@/stores/userInfo'
import { ElMessage } from 'element-plus'
import { InfoFilled, House, Document, ChatLineRound } from '@element-plus/icons-vue'
import CreateHotel from './components/CreateHotel.vue'

const router = useRouter()
const route = useRoute()
const userInfoStore = useUserInfoStore()
const hotelData = ref(null)
const loading = ref(true)
const error = ref(false)
const errorMessage = ref('未找到酒店信息')
const createHotelRef = ref()

// 是否为新酒店商家（无关联酒店）
const isNewHotelOwner = computed(() => {
  return userInfoStore.isHotelOwner() && error.value && 
         errorMessage.value.includes('该用户不是酒店商家或无关联酒店')
})

// 初始化数据
const initData = async () => {
  if (!userInfoStore.userInfo || !userInfoStore.isHotelOwner()) {
    errorMessage.value = '您没有访问此页面的权限'
    error.value = true
    loading.value = false
    return
  }
  
  loading.value = true
  error.value = false
  
  try {
    // 获取当前用户的酒店信息
    const userId = userInfoStore.userInfo.user.userId
    const res = await getHotelByUserId(userId)
    const result = res.data
    
    if (result && result.code === 200 && result.data) {
      hotelData.value = result.data
      
      // 如果是直接访问 /my-hotel 路径，自动跳转到详情页
      if (route.path === '/my-hotel') {
        router.push('/my-hotel/detail')
      }
    } else {
      error.value = true
      errorMessage.value = result?.msg || '获取酒店信息失败'
    }
  } catch (err) {
    console.error('获取酒店信息错误:', err)
    error.value = true
    errorMessage.value = '网络错误，获取酒店信息失败'
  } finally {
    loading.value = false
  }
}

// 导航到指定路由
const navigateTo = (path) => {
  router.push(path)
}

// 创建酒店
const openCreateHotelDrawer = () => {
  createHotelRef.value.open()
}

onMounted(() => {
  initData()
})
</script>

<style scoped>
.my-hotel-container {
  padding: 20px;
}

.loading-container, .error-container {
  min-height: 400px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.hotel-card {
  min-height: 500px;
}

.hotel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.hotel-title h1 {
  margin: 0;
  font-size: 24px;
}

.hotel-title p {
  margin: 5px 0 0;
  color: #606266;
}

.hotel-dashboard {
  margin-top: 40px;
}

.dashboard-card {
  height: 180px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.3s;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.dashboard-card:hover {
  transform: translateY(-5px);
}

.card-icon {
  font-size: 40px;
  margin-bottom: 10px;
  color: #409EFF;
}

.dashboard-card h3 {
  margin: 10px 0;
  color: #303133;
}

.dashboard-card p {
  color: #606266;
  margin: 0;
}
</style> 